<template>
  <BaseContainer>
    <aplid-base-crud :key="key" :paramsObj="paramsObj" ref="crud" class="w-100" v-bind="{...baseConfigData,...$attrs}">
      <!-- <template v-slot:menuRight v-if="!FacilityBaseConfig">
        <MessageList type="正在采集的数据" :dataType="5"></MessageList>
      </template> -->
      <template v-slot:importDialog="{ row, show }">
        <ImportForm @refresh="handleRefresh" :api="baseConfigData.api" :key="show" :nowRowData="row"></ImportForm>
      </template>

      <template v-slot:addDialog="{ row, show }">
        <TabForm @refresh="handleRefresh" :api="baseConfigData.api" :key="show" :nowRowData="row"></TabForm>
      </template>
      <template v-slot:RechangeDialog="{ row, show }">
        <Rechange @refresh="handleRefresh" :api="baseConfigData.api" :key="show" :nowRowData="row"></Rechange>
      </template>
      <template v-slot:ArchivesDialog="{ row, show }">
        <Archives @refresh="handleRefresh" @rechange="handleOperate" :api="baseConfig.api" :key="show" :nowRowData="row">
        </Archives>
      </template>

      <template v-slot:menuColumn="{ row }">
        <el-button v-if="row.dataStatus && row.dataStatus !== '0'" @click="handleChange(row)" text type="warning"> 变更中
        </el-button>
      </template>
    </aplid-base-crud>

    <el-dialog v-model="dialogVisible" title="数据变更" width="65%">
      <RechangeDetail v-if="dialogVisible" @refresh="handleRefresh" :batchNumber="nowData.dataStatus"></RechangeDetail>
    </el-dialog>

  </BaseContainer>
</template>

<script setup lang="ts">
import { baseConfig } from './config'
import ImportForm from './ImportForm/index.vue'
import TabForm from './TabForm/index.vue'
import Archives from './Archives/index.vue'
import Rechange from './Rechange/index.vue'
import RechangeDetail from '@/views/datacenter/components/DataGather/change/detail.vue'
import MessageList from '@/views/aplid/MessageList/index.vue'
import { editConfig } from '@/views/datacenter/comprehensiveQueries/utils/index'
import { cloneDeep } from 'lodash-es'

let baseConfigData :any= reactive({ api: {} })
// 养老设施中使用时会传输数据
type Props = {
  FacilityBaseConfig?: any,
  paramsObj?: any,
  isComprehensiveQueries?:boolean
}
const props = withDefaults(defineProps<Props>(), {
  FacilityBaseConfig: null,
  paramsObj: {},
  isComprehensiveQueries:false
})

let nowData: any = {}
let key = ref(1)

if (props.FacilityBaseConfig !== null) {
  baseConfigData = props.FacilityBaseConfig
  key.value = Math.random()
} else {
    if(baseConfig){
    let defaultConfig = cloneDeep(baseConfig)
    if(props.isComprehensiveQueries){
      baseConfigData = editConfig(defaultConfig)
    }else{
      baseConfigData = baseConfig
    }
  }
}

const crud = ref(null)

const dialogVisible = ref(false)
const handleRefresh = (query, visible = false) => {
  dialogVisible.value = visible
  nextTick(() => {
    crud.value.refreshChange(query)
  })
}
const handleChange = (row) => {
  nowData = row
  dialogVisible.value = true
}
const handleOperate = (type, row, title) => {
  nextTick(() => {
    crud.value.handleOperate(type, row, title)
  })
}
defineExpose({
  handleRefresh
})
</script>

<style lang="scss" scoped></style>
